package ru.spbau.database.group3.types;

/**
 * @author: stas
 * @date: 2/6/13
 * @time: 12:35 AM
 */
public enum Types {
    INT("INT") {
        @Override
        public Type forName(String s) {
            return new IntegerType();
        }
    },
    DOUBLE("DOUBLE") {
        @Override
        public Type forName(String s) {
            return new DoubleType();
        }
    },
    VARCHAR("VARCHAR") {
        @Override
        public Type forName(String s) {
            int length = Integer.parseInt(s.substring(s.indexOf('(') + 1, s.indexOf(')')));
            return new VarcharType(length);
        }
    };

    public abstract Type forName(String s);

    public static Types parseString(String s) {
        for(Types t : Types.values())  {
            if(s.contains(t.representation)) {
                return t;
            }
        }
        throw new IllegalArgumentException("There isn't type with name " + s);
    }

    private Types(String representation) {
        this.representation = representation;
    }

    private String representation;
}
